package com.hh.bss.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import net.sf.json.JSONObject;



import com.hh.bss.common.TreeNodeDTO;

public class JsonUtils {

	 private static ObjectMapper objectMapper = new ObjectMapper();;
	public static String toJsonString(List list){
		if(list == null){
			list = new ArrayList();
		}
		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("totalProperty",list.size());
		jsonObj.put("dataList", list);
		
		return jsonObj.toString();
	}
	
	public static List Json2List(String json,Object obj) throws IOException{
		//List<Object> list =null;
		List<Object> listA = objectMapper.readValue( json, new TypeReference<List<Object>>(){});
		return listA;

		
	}
	
	public static <T> T fromJson(String json, TypeReference<T> typeReference) {
        try {
                return (T) objectMapper.readValue(json, typeReference);
        } catch (Exception e) {
                throw new RuntimeException(e);
        }
}
	

	
	public static String toJsonString(List list ,int total){
		if(list == null){
			list = new ArrayList();
		}
		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("totalProperty",total);
		jsonObj.put("dataList", list);
		
		return jsonObj.toString();
	}
	
	public static String toJsonString(Object o){
		if(o == null){
			return "[{}]";
		}
		StringBuffer buffer = new StringBuffer();
		try {
			buffer.append("[{");
			Field[] fields = o.getClass().getDeclaredFields();
			for(int i=0 ; i<fields.length ; i++){
				Field field = fields[i];
				String methodName = "";
				if(field.getClass().equals(boolean.class) || field.getClass().equals(Boolean.class)){
					methodName = "is" + field.getName().substring(0,1).toUpperCase() + field.getName().substring(1);
				}else {
					methodName = "get" + field.getName().substring(0,1).toUpperCase() + field.getName().substring(1);
				}
				Method method = o.getClass().getMethod(methodName, new Class[]{});
				Object obj = method.invoke(o, new Class[]{});
				buffer.append("\"" + field.getName() + "\"");
				buffer.append(":");
				if(field.getClass().equals(String.class)){
					buffer.append("\"" + ((String)obj).trim() + "\"");
				}else{
					buffer.append("\"" + obj + "\"");
				}
				if(i != fields.length-1){					
					buffer.append(",");
				}
			}
			buffer.append("}]");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	
	public static String toJsonString(TreeNodeDTO obj , boolean tree){
		List<TreeNodeDTO> list = obj.getChildren();
		if(list == null){
			return "[]";
		}
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("[");
		for(int i=0 ; i<list.size() ; i++){
			TreeNodeDTO dto = list.get(i);
			buffer.append("{");
			buffer.append("\"text\":" + "\"" + dto.getText() + "\",");
			buffer.append("\"leaf\":" + dto.isLeaf());
			if(dto.getChildren()!= null){
				buffer.append(",\"children\":" + toJsonString(dto, true));
			}
			buffer.append("}");
			if(i != list.size()-1){
				buffer.append(",");
			}
		}
		buffer.append("]");
		return buffer.toString();
	}

}
